package com.yc.config;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * @program: cloud147
 * @description: idGenerator的负载均衡策略配置类
 * @author: zy
 * @create: 2025-07-13 15:16
 */
@Configuration
public class IdGeneratorLoadBalancerConfig {
    @Bean  // id="randomLoadBalancer" class=""
    public ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment,
                                                                   LoadBalancerClientFactory loadBalancerClientFactory) {
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        // 上面代码 :   environment.getProperty("loadbalancer.client.name") -> idGenerator

        return new RoundRobinLoadBalancer(  loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class)  , name);
    }
}
